﻿using Lamex.Base;
using Lamex.Library;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mSchedule.UI
{
    public partial class frmMES_BarcodeScanner : frmBaseChild
    {
        static int sums = 0;//统计当天记录数

        public frmMES_BarcodeScanner()
        {
            InitializeComponent();
        }
        private void frmMES_BarcodeScanner_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            txtWorkStation.Text = Lamex.Common.Loginer.CurrentUser.Account;//工作中心
            GetBarCode_SUMS();//统计当天记录数
            txtQty.Text = sums.ToString();
            txtBarcode.Focus();
            lblInfo.Text = "";
        }
        private void txtBarcode_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyValue == 13)
                {
                    //获取条码工艺路线
                    string strBarCode = txtBarcode.Text.Trim();
                    if (strBarCode.Length > 0)
                    {
                        string sql_Routing = @" select   substring(  (
                                                Select '-',case when w.IDNO is null then rtemp.WORKCENTER_NUMBER else w.NUMBER end   AS [text()]
                                                From [Lamex_MES].[dbo].[T3_ROUTING] rtemp WITH (NOLOCK) 
			                                    LEFT JOIN [Lamex_MES].[dbo].[T3_WORKSTATION] w WITH (NOLOCK) ON w.NUMBER = rtemp.WORKCENTER_NUMBER
                                                Where c.[ORDER] = rtemp.[ORDER]
                                                order by rtemp.PROCESS_STEP 
                                                For XML PATH ('')
                                                ), 2, 1000) PROCESS
		                                        from [Lamex_MES].[dbo].[T3_COMPONENT] c where c.PART_BARCODE='" + strBarCode + "'";
                        DataTable dtRouting = BaseBuildSql.GetDataTableBySql(sql_Routing);
                        if (dtRouting.Rows.Count > 0)
                        {
                            string strRouting = dtRouting.Rows[0][0].ToString().Trim();                        
                            //判断该站点是否经过条码的工艺路线
                            if (strRouting.Split('-').Contains(txtWorkStation.Text.Trim()))
                            {
                                //插入数据库                              
                                if (!Insert_Data(strBarCode))
                                {
                                    //DevExpress.XtraEditors.XtraMessageBox.Show("该条码已扫描,不能重复扫描！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    lblInfo.Text = "错误！不能重复扫描！";
                                    this.lblInfo.Appearance.ForeColor = System.Drawing.Color.Red;
                                    txtBarcode.Text = "";
                                    txtBarcode.Focus();
                                    return;
                                }
                                sums = sums + 1;
                                txtQty.Text = sums.ToString();
                                txtBarcode.Text = "";
                                txtBarcode.Focus();
                                lblInfo.Text = "保存成功！";
                                this.lblInfo.Appearance.ForeColor = System.Drawing.Color.Green;
                                GetBarCode_SUMS();
                            }
                            else
                            {
                                //DevExpress.XtraEditors.XtraMessageBox.Show("该条码不经过工艺路线【" + txtWorkStation.Text.Trim() + "】,请确认！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                lblInfo.Text = "错误！不经过【" + txtWorkStation.Text.Trim() + "】！";
                                this.lblInfo.Appearance.ForeColor = System.Drawing.Color.Red;
                                txtBarcode.Text = "";
                                txtBarcode.Focus();
                                return;
                            }
                        }
                        else
                        {
                            //DevExpress.XtraEditors.XtraMessageBox.Show("找不到该条码的工艺路线！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            lblInfo.Text = "错误！找不到工艺路线！";
                            this.lblInfo.Appearance.ForeColor = System.Drawing.Color.Red;
                            txtBarcode.Text = "";
                            txtBarcode.Focus();
                            return;
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void GetBarCode_SUMS()
        {
            string sql = @" SELECT *  FROM [LamexMESManager].[dbo].[MES_BarCode_ScannerInfo] where WORKSTATION='" + txtWorkStation.Text.Trim() + "' and INSERT_TS>='" + DateTime.Now.ToString("yyyy-MM-dd") + "' order by INSERT_TS desc";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            sums = Convert.ToInt32(dt.Rows.Count.ToString());
            gridControl1.DataSource = dt;
            gridView1.OptionsView.ColumnAutoWidth = false;
            this.gridView1.BestFitColumns();
        }
        public bool Insert_Data(string strBarCode)
        {
            try
            {
                string sql_Insert = @"insert into [LamexMESManager].[dbo].[MES_BarCode_ScannerInfo](
	                                                   [PART_BARCODE]
                                                      ,[WORKSTATION]
                                                      ,[INSERT_TS]
                                                      ,[INSERT_USER])
	                                                  values('" + strBarCode + "','" + txtWorkStation.Text.Trim() + "',GETDATE(),'" + Lamex.Common.Loginer.CurrentUser.AccountName + "')";
                BaseBuildSql.ExecuteNonQuery(sql_Insert);
                return true;
            }
            catch
            {

                return false;
            }
        }

        private void frmMES_BarcodeScanner_Resize(object sender, EventArgs e)
        {
            this.gridControl1.Width = this.panelControl2.Width / 2;
            this.groupControl1.Width = this.panelControl2.Width / 2;
            this.groupControl1.Height = this.panelControl2.Height / 2;
            this.groupControl2.Height = this.panelControl2.Height / 2;
        }

        private void groupControl1_Resize(object sender, EventArgs e)
        {
            this.txtQty.Location = new Point(Convert.ToInt32(groupControl1.Width - txtQty.Width) / 2, Convert.ToInt32(groupControl1.Height - txtQty.Height) / 4);
        }
    }
}
